#include "cadreedutab.h"
#include "ui_cadreedutab.h"
#include <QDialog>
#include <QGridLayout>
#include <QLineEdit>
#include <QSqlQuery>
#include <QPushButton>
#include <QSqlTableModel>
#include "timeeditdelegate.h"
#include <QDebug>
#include <QSqlRelationalTableModel>
#include <QSqlRelationalDelegate>
#include <QLabel>
#include <QHash>
#include <QDialogButtonBox>
#include <QMessageBox>
#include <QSqlError>
#include "cdateedit.h"
#include "cadreutil.h"

CadreEduTab::CadreEduTab(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CadreEduTab)
{
    ui->setupUi(this);
}

CadreEduTab::~CadreEduTab()
{
    delete ui;
}
//eduflag 区分显示是全日制(1)还是在职教育(0)
void CadreEduTab::initEduTableView(QString curPerson, QString eduflag)
{
    flag = eduflag;
    eduModel = new QSqlRelationalTableModel(this);
    eduModel->setTable("Edu");
    // EduStateID
    eduModel->setRelation(3, QSqlRelation("EduStateCode", "EduStateID", "EduState"));
    // EduName
    eduModel->setRelation(4, QSqlRelation("EducationCode", "EducationID", "Education"));
    // AcademicDe
    eduModel->setRelation(5, QSqlRelation("DegreeCode", "DegreeID", "Degree"));
    eduModel->setJoinMode(QSqlRelationalTableModel::LeftJoin);
    eduModel->setEditStrategy(QSqlTableModel::OnFieldChange);// OnRowChange OnFieldChange OnManualSubmit
    QString filter;
    if(eduflag=="1"){
        filter = QString::fromUtf8("IDNum='%1' and WorkORSchool='1'").arg(curPerson);
    }
    if(eduflag=="0"){
        filter = QString::fromUtf8("IDNum='%1' and WorkORSchool='0'").arg(curPerson);
    }
    currentPerson=curPerson;
    eduModel->setFilter(filter);
    eduModel->select();
    eduModel->setHeaderData(1, Qt::Horizontal, QString::fromUtf8("开始时间"));
    eduModel->setHeaderData(2, Qt::Horizontal, QString::fromUtf8("结束时间"));
    eduModel->setHeaderData(3, Qt::Horizontal, QString::fromUtf8("完成情况"));
    eduModel->setHeaderData(4, Qt::Horizontal, QString::fromUtf8("学历"));
    eduModel->setHeaderData(5, Qt::Horizontal, QString::fromUtf8("学位"));
    eduModel->setHeaderData(6, Qt::Horizontal, QString::fromUtf8("学校"));
    eduModel->setHeaderData(7, Qt::Horizontal, QString::fromUtf8("专业"));
    ui->eduTableView->horizontalHeader()->setVisible(true); //UI界面设置，总是恢复为false
    ui->eduTableView->setModel(eduModel);
    ui->eduTableView->setItemDelegate(new QSqlRelationalDelegate(ui->eduTableView));

    TimeEditDelegate *starttime = new TimeEditDelegate();
    ui->eduTableView->setItemDelegateForColumn(1,starttime);
    TimeEditDelegate *endtime = new TimeEditDelegate();
    ui->eduTableView->setItemDelegateForColumn(2,endtime);

    ui->eduTableView->setColumnHidden(0,true);
    ui->eduTableView->setColumnHidden(8,true);
    ui->eduTableView->setColumnHidden(9,true);
    ui->eduTableView->setColumnWidth(1,130);
    ui->eduTableView->setColumnWidth(2,130);
    ui->eduTableView->setColumnWidth(3,80);
    ui->eduTableView->setColumnWidth(4,150);
    ui->eduTableView->setColumnWidth(5,150);
    ui->eduTableView->setColumnWidth(6,150);
    ui->eduTableView->setColumnWidth(7,150);
    ui->eduTableView->selectRow(eduModel->rowCount()-1);
}
void CadreEduTab::setButtonEnable(bool i)
{
    ui->eduAddButton->setEnabled(i);
    ui->eduDeleteButton->setEnabled(i);
    ui->eduDownButton->setEnabled(i);
    ui->eduUpButton->setEnabled(i);
    if(i){
        ui->eduTableView->setEditTriggers(QAbstractItemView::DoubleClicked);
    }else{
        ui->eduTableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    }

}

void CadreEduTab::on_eduAddButton_clicked()
{
    QDialog *addDialog = new QDialog(this);
    addDialog->setWindowTitle(trUtf8("添加学历信息"));
    addDialog->setGeometry(700,300,900,400);
    addDialog->setContentsMargins(10,0,10,0);
    QGridLayout *gridLayout = new QGridLayout(addDialog);
    gridLayout->setMargin(10);
    QLabel *stimeLabel =new QLabel(trUtf8("开始时间"),addDialog);
    QLabel *etimeLabel=new QLabel(trUtf8("结束时间"),addDialog);
    QLabel *edustateLabel=new QLabel(trUtf8("完成情况"),addDialog);
    QLabel *eduLabel=new QLabel(trUtf8("学历"),addDialog);
    QLabel *degreeLabel=new QLabel(trUtf8("学位"),addDialog);
    QLabel *schoolLabel=new QLabel(trUtf8("学校"),addDialog);
    QLabel *majorLabel=new QLabel(trUtf8("专业"),addDialog);
    CDateEdit *stime = new CDateEdit(addDialog);
    stime->setNullable(true);
    stime->setDisplayFormat("yyyy-MM-dd");
    stime->setCalendarPopup(true);
    stime->setDate(QDate());
    CDateEdit *etime = new CDateEdit(addDialog);
    etime->setNullable(true);
    etime->setDisplayFormat("yyyy-MM-dd");
    etime->setCalendarPopup(true);
    etime->setDate(QDate());
    QComboBox *edustate = new QComboBox(addDialog);
    ehash =CadreUtil::initComboBox(edustate,"","EduStateCode","EduStateID","EduState");
    QComboBox *eduname = new QComboBox(addDialog);
    enamehash =CadreUtil::initComboBox(eduname,"","EducationCode","EducationID","Education");
    QComboBox *academicde = new QComboBox(addDialog);
    degreehash =CadreUtil::initComboBox(academicde,"","DegreeCode","DegreeID","Degree");
    QLineEdit *school = new QLineEdit(addDialog);
    QLineEdit *major = new QLineEdit(addDialog);

    gridLayout->addWidget(stimeLabel,0,0);
    gridLayout->addWidget(stime,0,1);
    gridLayout->addWidget(etimeLabel,0,2);
    gridLayout->addWidget(etime,0,3);
    gridLayout->addWidget(edustateLabel,1,0);
    gridLayout->addWidget(edustate,1,1);
    gridLayout->addWidget(eduLabel,1,2);
    gridLayout->addWidget(eduname,1,3);
    gridLayout->addWidget(degreeLabel,2,0);
    gridLayout->addWidget(academicde,2,1);
    gridLayout->addWidget(schoolLabel,2,2);
    gridLayout->addWidget(school,2,3);
    gridLayout->addWidget(majorLabel,3,0);
    gridLayout->addWidget(major,3,1);

    QPushButton *yesButton = new QPushButton(trUtf8("确定"));
    QPushButton *cancelButton = new QPushButton(trUtf8("取消"));

    QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Horizontal);
    buttonBox->addButton(yesButton, QDialogButtonBox::AcceptRole);
    buttonBox->addButton(cancelButton, QDialogButtonBox::RejectRole);

    gridLayout->addWidget(buttonBox,4,1,1,4,Qt::AlignHCenter);

    connect(buttonBox, SIGNAL(accepted()), addDialog, SLOT(accept()));
    connect(buttonBox, SIGNAL(rejected()), addDialog, SLOT(reject()));

    if(addDialog->exec()==QDialog::Accepted){
        QSqlQuery query;
        query.prepare("insert into Edu (`StartTime`, `Endtime`, `EduStateID`, `EduName`,"
                      " `AcademicDe`,`School`, `Major`, `WorkORSchool`, `IDNum`)"
                      " values(:stime, :etime, :edustate, :eduname, :academicde, "
                      ":school, :major, :workorschool, :idnum )");
        query.bindValue(":stime",stime->date());
        query.bindValue(":etime",etime->date());
        query.bindValue(":edustate",ehash.key(edustate->currentText()));
        query.bindValue(":eduname",enamehash.key(eduname->currentText()));
        query.bindValue(":academicde",degreehash.key(academicde->currentText()));
        query.bindValue(":school",school->text());
        query.bindValue(":major",major->text());
        query.bindValue(":workorschool",flag);
        query.bindValue(":idnum",currentPerson);
        if(query.exec()){
            QMessageBox::information(this,trUtf8("提示"),
               trUtf8("插入数据成功"),QMessageBox::Yes);
            initEduTableView(currentPerson,flag);
        }else{
            QString info = query.lastError().databaseText();
            qDebug()<< query.lastError().driverText();
            QMessageBox::information(this,trUtf8("插入数据库错误"),
               info,QMessageBox::Yes);
        }

    }else{

    }
}

void CadreEduTab::on_eduDeleteButton_clicked()
{
    CadreUtil::deleteRow(eduModel, ui->eduTableView);
}

QSqlTableModel* CadreEduTab::getOriginalModel()
{
    QSqlTableModel *originaleduModel = new QSqlTableModel(this);
    originaleduModel->setTable("Edu");

    QString filter;
    if(flag=="1"){
        filter = QString::fromUtf8("IDNum='%1' and WorkORSchool='1'").arg(currentPerson);
    }
    if(flag=="0"){
        filter = QString::fromUtf8("IDNum='%1' and WorkORSchool='0'").arg(currentPerson);
    }
    originaleduModel->setFilter(filter);
    originaleduModel->select();
    originaleduModel->setEditStrategy(QSqlTableModel::OnFieldChange);
    return originaleduModel;
}

void CadreEduTab::on_eduUpButton_clicked()
{
    QSqlTableModel *originaleduModel = getOriginalModel();
    int cur = CadreUtil::moveUpOne(originaleduModel,0,ui->eduTableView);
    initEduTableView(currentPerson,flag);
    ui->eduTableView->selectRow(cur);
}

void CadreEduTab::on_eduDownButton_clicked()
{
    QSqlTableModel *originaleduModel = getOriginalModel();
    int cur = CadreUtil::moveDownOne(originaleduModel,0,ui->eduTableView);
    initEduTableView(currentPerson,flag);
    ui->eduTableView->selectRow(cur);
}
